package com.czq.cn.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri.equals("/login")) {
            return true; // 放行登录请求
        }
        // 检查用户是否已认证
        if (request.getSession().getAttribute("SPRING_SECURITY_CONTEXT") == null) {
            response.sendRedirect("/login");
            return false;
        }
        return true;
    }
}